﻿using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

namespace DotNet.Helper
{
    /// <summary>
    /// 图片帮助类
    /// </summary>
    public class ImageHelper
    {
        #region 文字水印
        /// <summary>
        /// 给图片添加文字水印
        /// </summary>
        /// <param name="imageFile">图片路径</param>
        /// <param name="newpath">新图片保存路径</param>
        /// <param name="waterText">水印内容</param>
        /// <param name="position">水印绘制位置</param>
        /// <param name="rotate">旋转角度，默认为0，范围-360 <= rotate <= 360</param>
        public static void AddWaterMark(string imageFile, string newpath, string waterText, WaterMarkPosition position, int rotate = 0)
        {
            Image image = null;
            Graphics graphics = null;
            SolidBrush brush = null;

            try
            {
                if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile) || rotate < -360 || rotate > 360)
                {
                    return;
                }

                //创建一个图片对象用来装载要被添加水印的图片
                image = Image.FromFile(imageFile);
                //新建一个绘图
                graphics = Graphics.FromImage(image);

                graphics.SmoothingMode = SmoothingMode.HighQuality;                     //设置呈现质量
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;        //设置文字的呈现质量
                graphics.CompositingQuality = CompositingQuality.HighQuality;           //设置复合影像的呈现质量
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;      //设置插补模式

                //设置画笔
                brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
                //定义水印字体
                Font waterTextFont = new Font("微软雅黑", 16, FontStyle.Regular);
                //获取水印文字在该字体下的大小
                SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);

                #region 计算绘制水印位置
                float x = 10;
                float y = 10;

                switch (position)
                {
                    case WaterMarkPosition.Left_Top:
                        #region 左上角
                        {
                            x = 10;
                            y = 10;
                        }
                        #endregion
                        break;
                    case WaterMarkPosition.Right_Top:
                        #region 右上角
                        {
                            x = image.Width - waterTextSize.Width - 10;
                            y = 10;
                        }
                        #endregion
                        break;
                    case WaterMarkPosition.Left_Bottom:
                        #region 左下角
                        {
                            x = 10;
                            y = image.Height - waterTextSize.Height - 10;
                        }
                        #endregion
                        break;
                    case WaterMarkPosition.Right_Bottom:
                        #region 右下角
                        {
                            x = image.Width - waterTextSize.Width - 10;
                            y = image.Height - waterTextSize.Height - 10;
                        }
                        #endregion
                        break;
                    default:
                        #region 正中
                        {
                            x = (image.Width - waterTextSize.Width) / 2;
                            y = (image.Height - waterTextSize.Height) / 2;
                        }
                        #endregion
                        break;
                }
                #endregion

                #region 旋转
                if (rotate == 0)
                {
                    //不旋转则在计算出的xy点绘制
                    graphics.DrawString(waterText, waterTextFont, brush, x, y);
                }
                else
                {
                    //将xy轴中心点设为计算出的位置
                    graphics.TranslateTransform(x, y);
                    //旋转图片
                    graphics.RotateTransform(rotate);
                    //绘制水印
                    graphics.DrawString(waterText, waterTextFont, brush, 0, 0);
                    //重置中心点
                    graphics.TranslateTransform(-x, -y);
                    //重置旋转角度
                    graphics.ResetTransform();
                }
                #endregion

                image.Save(newpath, ImageFormat.Jpeg);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (image != null)
                    image.Dispose();
                if (graphics != null)
                    graphics.Dispose();
                if (brush != null)
                    brush.Dispose();
            }
        } 
        #endregion
    }

    #region 水印位置枚举
    /// <summary>
    /// 水印位置枚举
    /// </summary>
    public enum WaterMarkPosition
    {
        /// <summary>
        /// 左上角
        /// </summary>
        Left_Top = 1,
        /// <summary>
        /// 左下角
        /// </summary>
        Left_Bottom = 2,
        /// <summary>
        /// 右上角
        /// </summary>
        Right_Top = 3,
        /// <summary>
        /// 右下角
        /// </summary>
        Right_Bottom = 4,
        /// <summary>
        /// 中间
        /// </summary>
        Center = 5
    }; 
    #endregion
}
